package com.hgyc.mom.mes.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.mes.mapper.MesTeamMapper;
import com.hgyc.mom.mes.entity.MesTeam;
import com.hgyc.mom.mes.entity.MesTeamMember;
import com.hgyc.mom.mes.service.MesTeamMemberService;
import com.hgyc.mom.mes.vo.MesTeamVO;
import com.hgyc.mom.mes.service.MesTeamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import com.hgyc.mom.common.exception.BizException;
import org.springframework.util.CollectionUtils;

/**
 * 班组Service业务层实现
 *
 * @author fwj
 * @since 2025-07-01
 */
@Slf4j
@Service
public class MesTeamServiceImpl extends ServiceImpl<MesTeamMapper, MesTeam> implements MesTeamService {

    @Autowired
    private MesTeamMemberService mesTeamMemberService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveTeam(MesTeamVO mesTeamVO) {
        // TODO 其他逻辑
        save(mesTeamVO);

        //新增班组成员
        List<MesTeamMember> TeamMembers = mesTeamVO.getAddTeamMembers();
        if (!CollectionUtils.isEmpty(TeamMembers)) {
            TeamMembers.forEach(item -> {
                item.setId(null);
                item.setTeamId(mesTeamVO.getId());
            });
            mesTeamMemberService.saveBatch(TeamMembers);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTeam(MesTeamVO mesTeamVO) {
        // TODO 其他逻辑
        updateById(mesTeamVO);

        //删除班组成员
        if (!CollectionUtils.isEmpty(mesTeamVO.getDeleteTeamMemberIds())) {
            mesTeamMemberService.removeBatchByIds(mesTeamVO.getDeleteTeamMemberIds());
        }

        //新增班组成员
        List<MesTeamMember> TeamMembers = mesTeamVO.getAddTeamMembers();
        if (!CollectionUtils.isEmpty(TeamMembers)) {
            TeamMembers.forEach(item -> {
                item.setId(null);
                item.setTeamId(mesTeamVO.getId());
            });
            mesTeamMemberService.saveBatch(TeamMembers);
        }

        //更新班组成员
        if (!CollectionUtils.isEmpty(mesTeamVO.getUpdateTeamMembers())) {
            List<MesTeamMember> updateTeamMembers = mesTeamVO.getUpdateTeamMembers();
            mesTeamMemberService.updateBatchById(updateTeamMembers);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeTeam(List<Long> ids) {
        // TODO 其他逻辑
        removeByIds(ids);
        //删除班组成员
        mesTeamMemberService.deleteBatchByParentIds(ids);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<MesTeam> mesTeamList, boolean updateSupport) {
        if (mesTeamList == null || mesTeamList.isEmpty()) {
            throw new IllegalArgumentException("导入数据不能为空");
        }

        try {
            if (updateSupport) {
                // TODO 实现更新已有数据的导入逻辑
                return "更新导入成功，共导入 " + mesTeamList.size() + " 条数据";
            } else {
                // TODO 实现不更新已有数据的导入逻辑
                return "新增导入成功，共导入 " + mesTeamList.size() + " 条数据";
            }
        } catch (Exception e) {
            log.error("班组导入失败", e);
            throw new BizException("班组导入失败：" + e.getMessage());
        }
    }
}